const fs = require("fs");
const execa = require("execa");

// 获取打包文件
const dirs = fs.readdirSync("packages").filter((item) => {
  return fs.statSync(`packages/${item}`).isDirectory();
});

// 进行并行打包
const runParallel = async (dirs, iteratorFn) => {
  let result = [];

  for (let item of dirs) {
    result.push(iteratorFn(item));
  }

  return Promise.all(result);
};

const build = async (target) => {
  // console.log('$$$$$$$$$$$$$$$$$$$', target)

  // 开启子进程
  // -c 表示使用rollup.config.js配置文件   --environment环境变量
  await execa("rollup", ["-c", "--environment", `TARGET:${target}`]);
};

runParallel(dirs, build).then(() => {});

// console.log('$$$$', dirs)
